package cn.com.aiouyi.controller.admin;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.Accessory;
import cn.com.aiouyi.plugin.StoragePlugin;
import cn.com.aiouyi.plugin.localStorage.LocalStoragePlugin;
import cn.com.aiouyi.service.AccessoryService;
import cn.com.aiouyi.service.FileService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by tommy on 2018/4/26.
 */
@RestController("adminAccessoryController")
@RequestMapping("/admin/accessory")
public class AccessoryController extends BaseController {

    @Resource(name = "localStoragePlugin")
    private LocalStoragePlugin localStoragePlugin;

    @Resource(name = "fileServiceImpl")
    private FileService fileService;

    @Resource(name = "accessoryServiceImpl")
    private AccessoryService accessoryService;

    @PostMapping(value = "upload")
    public Message upload(MultipartFile file) throws IOException {
        if (file.getSize() > 0) {
            StringBuffer path = new StringBuffer(localStoragePlugin.generatePath());
            Message message = localStoragePlugin.upload(path.toString(), file.getOriginalFilename(), file.getInputStream(), StoragePlugin.FileType.text);
            return message;
        } else {
            return Message.error("请上传文件");
        }
    }

    @GetMapping(value = "download")
    public void download(Long fileId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("fileId={}", fileId);
        if (fileId != null) {
            cn.com.aiouyi.entity.File fileObj = fileService.find(fileId);
            Accessory accessory = fileObj.getAcc();
            logger.info("accessory={}", accessory);
            if (accessory != null){
                String realPath = accessory.getPath();

                //更新下载次数
                fileService.updateDownloadCount(fileObj);

                File file = new File(request.getServletContext().getRealPath(realPath));

                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition","attachment;filename=" + accessory.getOriginName());
                response.setContentLength((int) file.length());

                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file);
                    byte[] buffer = new byte[128];
                    int count = 0;
                    while ((count = fis.read(buffer)) > 0) {
                        response.getOutputStream().write(buffer, 0, count);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                    fis.close();
                }
            }
        }
    }
}
